package com.siqi.hr.system.organization.dao.mysql;

import com.siqi.hr.system.custom.entity.EntityRecord;
import com.siqi.hr.system.organization.dao.intf.MenuDao;
import com.siqi.hr.system.organization.dao.intf.OrganizeDao;
import com.siqi.hr.system.organization.dao.intf.RoleDao;
import com.siqi.hr.system.organization.dao.intf.RoleMenuDao;
import com.siqi.hr.system.organization.entity.Menu;
import com.siqi.hr.system.organization.entity.Role;
import com.siqi.hr.system.organization.entity.RoleMenu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 添加实体时添加权限菜单按钮dao实现类
 * @author     xiaogq
 * data         2016-08-30
 */
@Component("organizeDao")
public class OrganizeDaoImpl implements OrganizeDao{

    @Autowired
    private RoleDao roleDao;

    @Autowired
    private RoleMenuDao roleMenuDao;

    @Autowired
    private MenuDao menuDao;

    @Override
    public void settingEntityPermissions(EntityRecord entity) {
        //获取所有管理员角色
        List<Role> adminList = roleDao.getAllAdminRoles();
        if (adminList.size() == 0) {
            throw new RuntimeException("系统没有管理员");
        }
        //获取等级菜单最大的排序号
        Integer maxPosion = this.menuDao.getMenuLevelOneMaxPosition();
        String entityName = entity.getInchinese();
        Integer entityId = entity.getTableCode();
        Integer position = 0;
        Integer pid = menuDefaultSet(entity, entityName, 1, null,"universal_n.png",maxPosion = maxPosion + 1, null,true, adminList, null);
        menuDefaultSet(entity, "列表", 2, pid, null, position = position + 1,"CommonOperation/initCustomer.do?entityId=" + entityId + "&isTable=false", false,adminList, null);
        //如果是有回收站的实体
        if (entity.getUseRecyclebin() != null && entity.getUseRecyclebin()) {
            //menuDefaultSet(entity, "收藏夹", 2, pid, null,position = position + 1,"Favorite/initFavorite.do?entity_id=" + entityId + "&menuTitle=收藏夹：" + entityName,false, adminList, null);
            menuDefaultSet(entity, "回收站",2, pid, null,position = position + 1,"Recyclebin/initRecyclebin.do?entity_id=" + entityId + "&menuTitle=回收站：" + entityName,false, adminList, null);
        } else if (!entity.getSystemEntity()) {
            //menuDefaultSet(entity, "收藏夹",2, pid, null,position = position + 1,"Favorite/initFavorite.do?entity_id=" + entityId + "&menuTitle=收藏夹：" + entityName,false, adminList, null);
        }
        //保存角色、实体操作权限关系
        roleDao.setRoleEntityOperation(adminList, entity);
        //保存角色、实体数据权限关系
        roleDao.setDefaultRoleEntityScope(adminList, entity);
        //修改注册实体对象中模块id字段值
        //entityRecordDao.updateEntityBelongMenu(entityId, pid);
    }

    private Integer menuDefaultSet(EntityRecord entity, String name, Integer level, Integer pid, String icon,Integer position, String url, Boolean isParent,List<Role> rolelist, String belongModule) {
        Menu menu = new Menu();
        menu.setEntity(entity);
        menu.setName(name);
        menu.setLevel(level);
        menu.setPid(pid);
        menu.setIcon(icon);
        menu.setPosition(position);
        menu.setUrl(url);
        menu.setIsparent(isParent);
        menu.setFlag(true);
        menu.setBelongMudule(belongModule == null ? entity.getBelongMudule() : belongModule);
        int menuid = this.menuDao.addMenu(menu);
        menu.setId(menuid);
        for (int i = 0; i < rolelist.size(); i++) {
            RoleMenu roleMenu = new RoleMenu();
            roleMenu.setRole(rolelist.get(i));
            roleMenu.setMenu(menu);
            this.roleMenuDao.addRoleMenu(roleMenu);
        }
        return menu.getId();
    }


}
